package com.tjshop.android;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.tjshop.android.adapter.MyspinnerAdapter;
import com.tjshop.android.constants.UrlConfigs;
import com.tjshop.android.mcv.GlobeVars;
import com.tjshop.android.model.MyListItem;
import com.tjshop.android.model.tj.json.Address;
import com.tjshop.android.util.AddressUtil;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.PopupWindow.OnDismissListener;

public class AddAddressActivity extends BaseActivity implements
		View.OnClickListener {
	private EditText name;
	private EditText tel;
	private EditText detail;
	private FrameLayout add;
	private TextView selectRegions;
	private String type ;
	RelativeLayout rl_default;
	Address address;

	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_address);
		
		setTheme(R.style.Theme_Sherlock);
		
		type = getIntent().getExtras().getString("type");
		String title = "新增地址";
		if(type.equals("view")){
			title = "查看地址";
		}else if(type.equals("edit")){
			title = "编辑地址";
		}
		updateCommonReturnBar(title);
		
		
		name = (EditText) findViewById(R.id.add_address_name);
		tel = (EditText) findViewById(R.id.add_address_telNum);
		rl_default = (RelativeLayout) findViewById(R.id.rl_default);
		detail = (EditText) findViewById(R.id.add_address_detail);
		selectRegions = (TextView) findViewById(R.id.selectRegion_tv);
		selectRegions.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startActivityForResult(new Intent(getApplicationContext(), SelectRegionActivity.class), 2);
			}
		});
		add = (FrameLayout) findViewById(R.id.add_address_add);
	
		
		add.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				commit();
				
			}
		});
		
		address = (Address) getIntent().getExtras().getSerializable("data");
		if(type.equals("view")){
			add.setVisibility(View.GONE);
			rl_default.setVisibility(View.GONE);
			selectRegions.setText(address.getAddress());
			name.setText(address.getConsignee());
			tel.setText(address.getTel());
			tel.setEnabled(false);
			name.setEnabled(false);
			selectRegions.setClickable(false);
			selectRegions.setEnabled(false);
		}else if(type.equals("edit")){
			selectRegions.setText(address.getAddress());
			name.setText(address.getConsignee());
			tel.setText(address.getTel());
		}
	}
	private void commitEdit(){
		StringRequest stringRequest = new StringRequest(Method.GET,
				UrlConfigs.URL_EDIT_ADDRESS + "?t=" + GlobeVars.token + "&address_id=" + address.getAddress_id()+"&consignee="+
		name.getText().toString()+"&address",
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						JSONObject jsonU;
						try {
							jsonU = new JSONObject(response);
							if (jsonU.getInt("r") == 0) {
								Message message = new Message();
								message.what = 2;
								message.obj = jsonU.getString("t");
								mHandler.sendMessage(message);
							} else {
								Message message = new Message();
								message.what = 3;
								message.obj = jsonU.getString("m");
								mHandler.sendMessage(message);
							}
						} catch (JSONException e) {
							Message message = new Message();
							message.what = 1;
							message.obj = "服务器返回异常！";
							mHandler.sendMessage(message);
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						Message message = new Message();
						message.what = 1;
						mHandler.sendMessage(message);

					}

				});

		mQueue.add(stringRequest);
	}
	private void commitAdd(){
		StringRequest stringRequest = new StringRequest(Method.GET,
				UrlConfigs.URL_ADD_ADDRESS + "?t=" + GlobeVars.token + "&p=" + pass,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						JSONObject jsonU;
						try {
							jsonU = new JSONObject(response);
							if (jsonU.getInt("r") == 0) {
								Message message = new Message();
								message.what = 2;
								message.obj = jsonU.getString("t");
								mHandler.sendMessage(message);
							} else {
								Message message = new Message();
								message.what = 3;
								message.obj = jsonU.getString("m");
								mHandler.sendMessage(message);
							}
						} catch (JSONException e) {
							Message message = new Message();
							message.what = 1;
							message.obj = "服务器返回异常！";
							mHandler.sendMessage(message);
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						Message message = new Message();
						message.what = 1;
						mHandler.sendMessage(message);

					}

				});

		mQueue.add(stringRequest);
	}
	private void commit() {
		if(type.equals("add")){
			commitAdd();
		}else if(type.equals("edit")){
			commitEdit();
		}
	}
	
	@Override
	public void onClick(View arg0) {
	}

	@Override
	public boolean handleMessage(Message arg0) {
		return false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        switch (requestCode) {  
            case 2:  
            	if(data!=null){
                  this.selectRegions.setText(data.getStringExtra("region"));
            	}
  
                break;  
        }  
    }  
}
